<template>
	<el-dialog :title="titleMap[mode]" v-model="visible" :width="500" destroy-on-close @closed="$emit('closed')">
		<el-form :model="form" :rules="rules" :disabled="mode=='show'" ref="dialogForm" label-width="100px">
			<el-form-item label="业务key" prop="biz">
				<el-input v-model="form.biz" disabled placeholder="请输入业务key" clearable></el-input>
			</el-form-item>
			<el-form-item v-if="form.type == 2" label="配置值" prop="avatar">
				<sc-upload v-model="form.biz_value" title="上传图片"></sc-upload>
			</el-form-item>
			<el-form-item v-else-if="form.type == 3" label="配置值" prop="avatar">
				<sc-editor v-model="form.biz_value" placeholder="请输入" :templates="templates" :height="400"></sc-editor>
			</el-form-item>
			<el-form-item label="配置值" v-else prop="biz_value">
				<el-input v-model="form.biz_value" placeholder="请输入配置值" clearable></el-input>
			</el-form-item>
			<el-form-item label="备注" prop="remark">
				<el-input v-model="form.remark" placeholder="备注" clearable></el-input>
			</el-form-item>
		</el-form>
		<template #footer>
			<el-button @click="visible=false" >取 消</el-button>
			<el-button v-if="mode!='show'" type="primary" :loading="isSaveing" @click="submit()">保 存</el-button>
		</template>
	</el-dialog>
</template>

<script>
	import { defineAsyncComponent } from 'vue';
	const scEditor = defineAsyncComponent(() => import('@/components/scEditor'));
	import {filterAsyncImgs} from '@/utils/tools'
	export default {
		emits: ['success', 'closed'],
		components: {
			scEditor
		},
		data() {
			return {
				mode: "add",
				titleMap: {
					add: '新增',
					edit: '编辑',
					show: '查看'
				},
				visible: false,
				isSaveing: false,
				//表单数据
				form: {
					id:"",
					biz: "",
					biz_value: "",
					remark: "",
					type: 1
				},
				//验证规则
				rules: {
					biz_value: [
						{required: true, message: '请输入配置值'}
					]
				},
				//所需数据选项
				groups: [],
				groupsProps: {
					value: "id",
					emitPath: false,
					checkStrictly: true
				},
				templates: [
					{
						title: '自定义HTML模板',
						description: '',
						content: '<strong>演示模板</strong>'
					},
					{
						title: '列表',
						description: '',
						content: '<ol><li>演示列表1</li><li>演示列表2</li></ol>'
					}
				]
			}
		},
		mounted() {

		},
		methods: {
			//显示
			open(mode='add'){
				this.mode = mode;
				this.visible = true;
				return this
			},
			//表单提交方法
			async submit(){
				var valid = await this.$refs.dialogForm.validate().catch(() => {})
				if(!valid){
					return false
				}
				this.isSaveing = true;
				var res = await this.$API.system.configure.configUpdate.post(this.form);
				this.isSaveing = false;
				if(res.code == 200){
					if(this.mode=='add'){
						this.form.id = res.data
					}
					let form = JSON.parse(JSON.stringify(this.form))
					if (this.form.type == 2) form.biz_value = filterAsyncImgs([form.biz_value], 'path', 'url')[0].url
					this.$emit('success', form, this.mode)
					this.visible = false;
					this.$message.success("操作成功")
				}else{
					this.$alert(res.message, "提示", {type: 'error'})
				}
			},
			//表单注入数据
			setData(data){
				this.form.id = data.id
				this.form.biz = data.biz
				this.form.biz_value = data.biz_value
				this.form.remark = data.remark
				this.form.type = data.type
				//可以和上面一样单个注入，也可以像下面一样直接合并进去
				//Object.assign(this.form, data)
			}
		}
	}
</script>

<style>
</style>
